package org.eclipsercp.hyperbola.editors;

import java.text.MessageFormat;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

public class ChatEditor extends EditorPart {

	public static final String ID = "org.eclipsercp.hyperbola.editors.ChatEditor"; //$NON-NLS-1$
	private SashForm sashForm;
	private Text txtTranscript;
	private Composite composite;
	private ToolBar toolBar;
	private Text txtEntry;
	private Composite compButtonsBar;
	private Button btnSend;
	private Button btnClose;
	private ToolItem tltmFace;

	public ChatEditor() {
	}

	/**
	 * Create contents of the editor part.
	 * 
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		sashForm = new SashForm(parent, SWT.VERTICAL);

		txtTranscript = new Text(sashForm, SWT.BORDER | SWT.READ_ONLY
				| SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);

		composite = new Composite(sashForm, SWT.BORDER);
		composite.setLayout(new GridLayout(1, false));

		toolBar = new ToolBar(composite, SWT.FLAT | SWT.RIGHT);
		toolBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		tltmFace = new ToolItem(toolBar, SWT.NONE);
		tltmFace.setText("Face");

		txtEntry = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL
				| SWT.MULTI);
		txtEntry.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.character == SWT.CR) {
					sendMessage();
					e.doit = false;
				}
			}
		});
		txtEntry.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));

		compButtonsBar = new Composite(composite, SWT.NONE);
		GridLayout gl_compButtonsBar = new GridLayout(2, true);
		gl_compButtonsBar.marginHeight = 0;
		compButtonsBar.setLayout(gl_compButtonsBar);
		compButtonsBar.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));

		btnClose = new Button(compButtonsBar, SWT.NONE);
		btnClose.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		btnClose.setText("关闭");

		btnSend = new Button(compButtonsBar, SWT.NONE);
		GridData gd_btnSend = new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1);
		gd_btnSend.widthHint = 80;
		btnSend.setLayoutData(gd_btnSend);
		btnSend.setText("发送");
		sashForm.setWeights(new int[] { 6, 4 });
	}

	@Override
	public void setFocus() {
		if (null != txtEntry && !txtEntry.isDisposed()) {
			txtEntry.setFocus();
		}
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// Do the Save operation
	}

	@Override
	public void doSaveAs() {
		// Do the Save As operation
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);
		setPartName(getUser());
	}

	private String getUser() {
		return ((ChatEditorInput) getEditorInput()).getName();
	}

	private String renderMessage(String from, String body) {
		if (null == from) {
			return body;
		}

		int i = from.indexOf("@");
		if (i > 0) {
			from = from.substring(0, i);
		}

		return MessageFormat.format("<{0}> {1}", from, body);
	}

	private void scrollToEnd() {
		int charCount = txtTranscript.getCharCount();
		txtTranscript.setSelection(charCount, charCount);
		txtTranscript.showSelection();
	}

	private void sendMessage() {
		String body = txtEntry.getText();
		if (body.isEmpty()) {
			return;
		}

		txtTranscript.append(renderMessage(getUser(), body));
		txtTranscript.append("\n");
		scrollToEnd();
		txtEntry.setText("");
	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}
}
